//index.js
//获取应用实例
const app = getApp()
var appData = app.globalData
var sliderWidth = 106; // 需要设置slider的宽度，用于计算中间位置
Page({
    data: {
        tabs: ["挖钻", "圈态", "密圈"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        flat_tp: true, //投票
        flat_sh: true, //赎回

        error_sh: true, //赎回错误框
        error_sh_msg: "错误信息", //赎回错误信息

        error_tp: true, //投票错误框
        error_tp_msg: "错误信息", //投票错误信息

        isJoin: false, //是否加入
        user_id: '', // 用户ID
        master_id: '', // 圈主ID
        group_id: '', // 圈子ID
        owner: false, // 是否是管理员
        hasjoin: false, // 是否已加入圈子
        group_info: { // 圈子信息
            name: '',
            member_num: 0,
            photo: '',
            percent: 0,
            lqc_num: 0,
            popularity: 0,
            vote_num: 0,
            reward_num: 0
        },
        newPercent: '', // 新的百分比分红
    },
    onLoad: function (options) {
        var that = this
        var _this = this
        let group_id = options.group_id
        let master_id = options.master_id
        let user_id = wx.getStorageSync('user_id')
        console.log(master_id)
        console.log(user_id)
        _this.setData({
            master_id: master_id,
            group_id: group_id,
            user_id: user_id,
            owner: master_id == user_id ? true : false
        })
        // 获取系统信息
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                })
            }
        })
        // 获取圈子信息
        wx.request({
            url: appData.apiUrl + 'user/groupinfo',
            data: {
                group_id: group_id 
            },
            success: function (res) {
                console.log('获取圈子信息')
                console.log(res.data)
                if (res.data.status == true) {
                    _this.setData({
                        group_info: res.data.data
                    })
                }
            }
        })
    },
    // 加入圈子
    joinLing: function () {
        console.log('加入圈子')
        let _this = this
        let user_id = this.data.user_id
        let group_id = this.data.group_id
        wx.request({
            url: appData.apiUrl + 'user/joingroup',
            data: {
                user_id: user_id,
                group_id: group_id
            },
            success: function (res) {
                console.log(res)
                if (res.data.code == 0) {
                    _this.setData({ hasJoin: true })
                    wx.showToast({
                        title: res.data.data,
                        icon: 'success',
                        duration: 1000
                    })
                } else {
                    wx.showToast({
                        title: res.data.data,
                        icon: 'none',
                        duration: 1000
                    })
                }
            }
        })
    },
    // 返回
    toIndex: function () {
        console.log('返回首页')
        wx.navigateTo({
            url: '/pages/index/index',
        })
    },
    //tab
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        })
    },
    // 转发
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
            // console.log(res.target)
        }
        return {
            title: '一起来挖矿',
            path: '/pages/lingEnter/lingEnter?master_id=' + this.data.master_id + '&group_id=' + this.data.group_id
        }
    },
    // 投票模态框
    model_tp:function () {
        this.setData({
            flat_tp: false
        })
    },
    //关闭 投票弹窗
    close_tp: function () {
        this.setData({
            flat_tp: true
        })
    },
    model_sh: function () {
        this.setData({
            flat_sh: false
        })
    },
    //关闭 投票弹窗
    close_sh: function () {
        this.setData({
            flat_sh: true
        })
    },
    // 投票表单
    formSubmitTp: function (e) {
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
    },
    
    // 赎回表单
    formSubmitSh: function (e) {
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
    }, 
    //跳转到管理页
    goToGl: function () {
        let group_id = this.data.group_id
        let user_id = this.data.user_id
        wx.navigateTo({
            url: '../manager/manager?user_id=' + user_id + '&group_id=' + group_id
        })
    },
    goToIndex: function () {
        app.goToIndex()
    },
    //圈码
    goToCode: function () {
        let group_id = this.data.group_id
        let user_id = this.data.user_id
        wx.navigateTo({
            url: '/pages/code/code?user_id=' + user_id + '&group_id=' + group_id
        })
    },
})
